import os

def change_ip_bynetplan(ip_address, subnet_mask, gateway):
    data = f'''
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      addresses: [{ip_address}/{subnet_mask}]
      gateway4: {gateway}
      nameservers:
        addresses: [8.8.8.8, 8.8.4.4]
'''

    with open('eth0.yaml', 'w') as file:
        file.write(data)
        file.flush()

    # 移动生成后的eyh0.yaml到/etc/netplan中并替换原本的eth0.yaml
    os.system("sudo cp eth0.yaml /etc/netplan/eth0.yaml")

    #重启系统
    os.system("sudo reboot")

if __name__ == "__main__":

    ip_address = "192.168.88.100"
    subnet_mask = "24"
    gateway = "192.168.88.1"
    change_ip_bynetplan(ip_address, subnet_mask, gateway)
